﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SmartProject.BusinessRules
{
    public class RemoveWorkReferences : BusinessRule
    {
        public override string Name
        {
            get { return "Remove all references related to a work item."; }
        }

        public override string Description
        {
            get { return "Remove all references related to a work item."; }
        }

        public override void Implement(Entity data)
        {
            if (data.StoredProc == "smartProject_RemoveWork")
            {
                int workId = (int)data.GetAttributeValue("WorkId");
                BusinessObjects.Message message = new BusinessObjects.Message();
                message.SetAttribute("ReferenceObjectId", workId);
                List<int> messageIdList = message.LoadWorkMessageIdList();

                // Remove user messages first before removing the work messages.
                BusinessObjects.UserMessage userMessage = new BusinessObjects.UserMessage();
                for (int i = 0; i < messageIdList.Count; i++)
                {
                    userMessage.SetAttribute("MessageId", messageIdList[i]);
                    userMessage.Remove();
                }

                message.SetAttribute("ReferenceObjectId", workId);
                message.RemoveWorkMessages();
            }
        }

        public override bool QueryBooleanProperty(string propertyName)
        {
            return true;
        }
    }
}
